package com.guodi.model.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.ExcelUtil;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.secure.utils.AuthUtil;
import com.guodi.core.tool.api.R;
import com.guodi.core.tool.utils.Func;
import com.guodi.model.dto.ApiDivisionToScoreValueDto;
import com.guodi.model.entity.IdtScoreValue;
import com.guodi.model.service.*;
import com.guodi.model.vo.IdtScoreValueVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

/**
 * @描述：指标得分值表控制类
 * @作者: lzy
 * @日期: 2023-04-03
 */
@RestController
@RequestMapping("/IdtScoreValue")
@Api(value = "API - IdtScoreValueController", description = "指标得分值表")
public class IdtScoreValueController extends BaseController {

    @Resource
    public IIdtScoreValueService idtScoreValueService;

    /**
     * @描述:新增
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/4/3 17:06
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public R save(@Valid IdtScoreValue idtScoreValue){
        prepareData(idtScoreValue, true);
        idtScoreValueService.save(idtScoreValue);
        return R.data(idtScoreValue);
    }

    /**
     * @描述:修改
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/4/3 17:22
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(IdtScoreValue idtScoreValue) {
        prepareData(idtScoreValue, false);
        idtScoreValueService.updateById(idtScoreValue);
        return R.data(idtScoreValue);
    }

    /**
     * @描述:删除
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/4/3 17:22
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(String ids) {
        List<String> idList = Func.toStrList(ids);
        boolean b = idtScoreValueService.removeByIds(idList);
        return R.data(b);
    }

    /**
     * @描述:根据实体属性分页查询
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/4/3 17:12
     **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = true, dataType = "long", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "year", value = "年份", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "divisionCode", value = "行政区划", required = false, dataType = "int", paramType = "query"),
    })
    public R listByEntity(@ApiIgnore Page page, @ApiIgnore IdtScoreValue idtScoreValue){
        LambdaQueryWrapper<IdtScoreValue> wrapper = new QueryWrapper().lambda();
        if(idtScoreValue.getYear() != null){
            wrapper.eq(IdtScoreValue::getYear,idtScoreValue.getYear());
        }
        if(idtScoreValue.getDivisionCode() != null){
            wrapper.eq(IdtScoreValue::getDivisionCode,idtScoreValue.getDivisionCode());
        }
        IPage<IdtScoreValue> iPage = idtScoreValueService.page(page, wrapper);
        return R.data(iPage);
    }


    /**
     * @描述:通过年份和行政区查询指标体系得分情况
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/4/3 17:31
     **/
    @PostMapping("/getDataListByYearAndCode")
    @ApiOperation(value = " 通过年份和行政区查询指标体系得分情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = true, dataType = "long", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "year", value = "年份", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "divisionCode", value = "行政区划", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "指标体系编码", required = true, dataType = "string", paramType = "query"),
    })
    public R getDataListByYearAndCode(@ApiIgnore IdtScoreValue idtScoreValue, String code){
        Map result = idtScoreValueService.getDataListByYearAndCode(idtScoreValue,code);
        return R.data(result);
    }

    @RequestMapping(value = "/importExcel",method = RequestMethod.POST)
    @ApiOperation(value = "获取上传的指标表格")
    public R importExcel(MultipartFile file) {
        if(Objects.isNull(file)){
            return R.fail(StatusCode.ERROR_CODE_40005, "传入文件为空");
        }
        List<IdtScoreValueVo> r = getTargetTemplateData(file);
        List<IdtScoreValue> result = new ArrayList<>();
        for(IdtScoreValueVo vo : r){
            IdtScoreValue value = new IdtScoreValue();
            BeanUtils.copyProperties(vo,value);
            value.setId(UuidUtil.get32UUID());
            result.add(value);
        }
        idtScoreValueService.saveBatch(result);
        return R.data("");
    }

    private List<IdtScoreValueVo> getTargetTemplateData(MultipartFile file) {
        List<IdtScoreValueVo> excelDatas = ExcelUtil.importExcel(file, 0, 1, IdtScoreValueVo.class);

        return excelDatas;
    }


    private void prepareData(IdtScoreValue entity, boolean isSave) {
        if(isSave){
            entity.setId(UuidUtil.get32UUID());
            entity.setCreator(String.valueOf(getUser().getUserId()));
            entity.setCreateTime(new Date());
            //防止误修改
            entity.setModifier(null);
            entity.setModifyTime(null);
        }else{
            entity.setModifier(String.valueOf(getUser().getUserId()));
            entity.setModifyTime(new Date());
            //防止误修改
            entity.setCreator(null);
            entity.setCreateTime(null);
        }
    }

}
